package main

import (
	"fmt"
	"gin-ranking/common/zinx/ziface"
	"gin-ranking/common/zinx/znet"
)

// 基于Zinx框架开发的服务器端应用程序

// PingRouter ping test 自定义路由
type PingRouter struct {
	znet.BaseRouter
}

// PreHandle Test
func (p *PingRouter) PreHandle(request ziface.IRequest) {
	data := request.GetData()
	fmt.Println("Call Router PreHandle...", string(data))
	//_, err := request.GetConnection().GetTCPConnection().Write([]byte("before ping\n"))
	//if err != nil {
	//	fmt.Println("call back before ping error")
	//}
}

// Handle Test
func (p *PingRouter) Handle(request ziface.IRequest) {
	fmt.Println("Call Router Handle...")
	//_, err := request.GetConnection().GetTCPConnection().Write([]byte("ping...ping...ping\n"))
	//if err != nil {
	//	fmt.Println("call back ping...ping...ping error")
	//}
}

// PostHandle Test PostHandle
func (p *PingRouter) PostHandle(request ziface.IRequest) {
	fmt.Println("Call Router PostHandle...")
	//_, err := request.GetConnection().GetTCPConnection().Write([]byte("after ping\n"))
	//if err != nil {
	//	fmt.Println("call back after ping error")
	//}
}

func main() {
	// 1 创建一个server句柄, 使用Zinx的api
	server := znet.NewServer("[Zinx V0.3]")
	// 2 给当前zinx框架添加一个自定义的router
	server.AddRouter(1, &PingRouter{})
	// 3 启动server
	server.Server()
}
